package ee.videorentalstore.protocols.commands.filmmanagercommands;

import java.util.List;

import ee.videorentalstore.inventories.Film;
import ee.videorentalstore.inventories.FilmInventory;
import ee.videorentalstore.inventories.FilmType;
import ee.videorentalstore.protocols.commands.Command;

public class ChangeTheTypeOfAFilmCommand extends Command
{
	private final int FILM_ID_POSITION_IN_SPLITTED_INSTRUCTIONS_LIST = 1;
	private final int FILM_TYPE_POSITION_IN_SPLITTED_INSTRUCTIONS_LIST = 2;
	private final String EMPTY_STRING = "";
	
	
	@Override
	public String getCommandDescription()
	{
		return "CHANGE THE TYPE OF A FILM";
	}
	
	
	@Override
	public void executeCommand(String inputStreamInstructionsToProcess) 
	{ 
		List<String> splittedInstructionsList = 
				getSplittedInputStreamInstructionsToProcess(inputStreamInstructionsToProcess);
		Integer filmID = getSpecificIntegerValueOutOfSplittedInstructionsList(
				splittedInstructionsList, FILM_ID_POSITION_IN_SPLITTED_INSTRUCTIONS_LIST);
		
		if (filmID != null && getFilmTypeFromSplittedInstructions(splittedInstructionsList) != null)
		{
			Film filmWhichTypeToChange = FilmInventory.getInstance().getFilmByID(
					filmID.intValue());
			boolean filmExists = (filmWhichTypeToChange != null);
			if (filmExists)
			{
				filmWhichTypeToChange.setType(getFilmTypeFromSplittedInstructions(splittedInstructionsList));
				System.out.println("The film type has been successfully changed!");
			}
			else
			{
				System.out.println("The film with that kind of id does not exist!");
			}
		}
		else
		{
			System.out.println("Command could not be executed due to inserted instructions errors!");
		}
	}
	
	
	private FilmType getFilmTypeFromSplittedInstructions(List<String> splittedInstructionsList)
	{
		String filmTypeInString = null;
		try 
		{
			filmTypeInString = splittedInstructionsList.get(
					FILM_TYPE_POSITION_IN_SPLITTED_INSTRUCTIONS_LIST).trim();
		} catch (Exception e) {}
		
		if (filmTypeInString != null && !filmTypeInString.equals(EMPTY_STRING))
		{
			if (filmTypeInString.trim().equalsIgnoreCase("NEW RELEASE"))
			{
				return FilmType.NEW_RELEASE;
			}
			else if (filmTypeInString.trim().equalsIgnoreCase("REGULAR FILM"))
			{
				return FilmType.REGULAR_FILM;
			}		
			else if (filmTypeInString.trim().equalsIgnoreCase("OLD FILM"))
			{
				return FilmType.OLD_FILM;
			}
			else
			{
				return null;
			}
		}
		else
		{
			return null;
		}
	}
}
